import { ICustomer, ListRequest } from './data.d';

const data = [];

const educationList = ['博士', '硕士', '研究生', '本科', '专科'];

const randomNumber = (min: number, max: number) => {
  const num = Math.random() * (max - min) + min;
  const res = parseInt(`${num}`, 10);
  return Number(res);
};

for (let i = 0; i < 46; i++) {
  data.push({
    id: `${i}`,
    name: `Edward King ${i}`,
    age: 10 + i,
    sex: i % 2 === 0 ? 'man' : 'woman',
    address: `London, Park Lane no. ${i}`,
    phone: `${110}`,
    education: educationList[randomNumber(0, 4)],
  });
}

export const customers = data;

export const books = [
  {
    title: 'The Awakening',
    author: 'Kate Chopin',
  },
  {
    title: 'City of Glass',
    author: 'Paul Auster',
  },
];

export class List {
  mockCustomers: ICustomer[] = [...customers];

  static instance: any = null;
  static getInstance() {
    if (!List.instance) {
      List.instance = new List();
    }
    return List.instance;
  }
  getCustomers(params: ListRequest) {
    const { name, address, age, page, pageSize } = params;
    let newcustomers = this.mockCustomers.filter((item) => {
      const bool1 = !name ? true : (name && item.name.includes(name)) || item.name === name;
      const bool2 = !address
        ? true
        : (address && item.address?.includes(address)) || item.address === address;
      const bool3 = !age ? true : (age && item.age === age) || item.age === age;

      return bool1 && bool2 && bool3;
    });

    // 当前在第几页
    const newPage = page || 1;
    // 每页多少条
    const newpageSize = pageSize || 10;
    // 总条数
    const total = newcustomers.length;
    // 总页数
    const totalPages = Number((total / newpageSize).toFixed(0)) + (total % newpageSize);

    /*
    第1页 每页10条     newcustomers = list.slice(0, 10);
    第2页 每页10条     newcustomers = list.slice(11, 20);
    第3页 每页10条     newcustomers = list.slice(21, 30);
    第4页 每页10条     newcustomers = list.slice(31, 40);
    第5页 每页10条     newcustomers = list.slice(41, 50);
    第6页 每页10条     newcustomers = list.slice(51, 60);
    */

    //  需要截取的数组的结束下标
    const lastIndex = newPage * newpageSize;

    // 需要截取的数组的开始下标
    const startIndex = lastIndex - newpageSize;

    newcustomers = newcustomers.slice(startIndex, lastIndex);

    const result = {
      code: 200,
      message: null,
      data: newcustomers,
      pageInfo: {
        page: newPage,
        pageSize: newpageSize,
        total,
        totalPages,
      },
    };

    return result;
  }
  getDetailCustomer(id: string) {
    const datas = this.mockCustomers.filter((item: ICustomer) => `${item.id}` === `${id}`);

    let res = null;
    if (datas.length > 0) {
      res = datas[0];
    }

    console.log('---------newcustomers', res);

    return {
      code: 200,
      message: null,
      data: res,
    };
  }
  addCustomer(customer: ICustomer) {
    const num = (Math.random() * 1000000).toFixed(0);
    const id = `${num}`;
    this.mockCustomers.unshift({ ...customer, id });
    return {
      code: 200,
      message: null,
      data: null,
    };
  }
  removeCustomer(id: string) {
    this.mockCustomers = this.mockCustomers.filter((item: ICustomer) => `${item.id}` !== `${id}`);

    return {
      code: 200,
      message: null,
      data: null,
    };
  }
  updateCustomer(customer: ICustomer) {
    this.mockCustomers = this.mockCustomers.map((item: ICustomer) => {
      return `${customer.id}` === `${item.id}` ? customer : item;
    });
    console.log('---------newcustomers', this.mockCustomers);
    return {
      code: 200,
      message: null,
      data: null,
    };
  }
}
